package com.sphd.restaurantsystem.activitys.kuncun;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.InOrOutRecordAdapter;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.InorOutRecordInfo;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.InOrOutRecordPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.InOrOutRecordView;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *  库存
 */
public class InOrOutRecordActivity extends BaseActivity implements InOrOutRecordView, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.btn_search)
    TextView btnSearch;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    InOrOutRecordAdapter inOrOutRecordAdapter;
    InOrOutRecordPresenterImpl inOrOutRecordPresenter;
    private RestaurantInfo restaurantInfo;
    private LoginInfo loginInfo;
    Intent intent;
    Bundle bundle;
    private int curpage = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_or_out_record);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                    bundle.putSerializable(Constant.INOROUTRECODR,(InorOutRecordInfo)baseQuickAdapter.getItem(i));
                    intent.putExtras(bundle);
                    intent.setClass(InOrOutRecordActivity.this,GoodsActivity.class);
                    startActivity(intent);

            }
        });
        mySwf.setOnRefreshListener(this);


    }

    private void initView() {
        setTitleBar(toolbar, "当前库存");
        intent = getIntent();
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        loginInfo = DataSupport.findLast(LoginInfo.class);
        inOrOutRecordPresenter=new InOrOutRecordPresenterImpl(this);
        initAdapter();
        addData();
    }

    private void addData() {
        // TODO: 2016/12/26
        inOrOutRecordPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.INOROUTRECORD + "cgid=" +
                restaurantInfo.getId() + "&curpage=" + curpage + "&pageno=10");
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecy);
        inOrOutRecordAdapter=new InOrOutRecordAdapter(R.layout.item_in_out_record, new ArrayList<>());
        myRecy.setAdapter(inOrOutRecordAdapter);

    }

    @OnClick(R.id.btn_search)
    public void onClick() {

        intent.setClass(context,DateQueryActivity.class);
        startActivity(intent);
    }

    @Override
    public void recList(List<InorOutRecordInfo> infoList) {
        if (curpage==1){
            inOrOutRecordAdapter.setNewData(infoList);
        }else {
            inOrOutRecordAdapter.addData(infoList);
        }
        if (infoList.size()!=10){
            inOrOutRecordAdapter.loadComplete();
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
        mySwf.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        curpage=1;
        addData();
    }
}
